<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>商品管理</title>
    <!-- Tell the browser to be responsive to screen width -->
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <link rel="stylesheet" href="../plugins/bootstrap/css/bootstrap.min.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/AdminLTE.css">
    <link rel="stylesheet" href="../plugins/adminLTE/css/skins/_all-skins.min.css">
    <link rel="stylesheet" href="../css/style.css">
	<script src="../plugins/jQuery/jquery-2.2.3.min.js"></script>
    <script src="../plugins/bootstrap/js/bootstrap.min.js"></script>

</head>

<body class="hold-transition skin-red sidebar-mini" >
  <!-- .box-body -->

  <div class="box-header with-border">
	  <h3 class="box-title">商品审核</h3>
  </div>
  <div class="box-body">
	  <!-- 数据表格 -->
	  <div class="table-box">
		  <!--工具栏-->
		  <div class="pull-left">
			  <div class="form-group form-inline">
				  <div class="btn-group">
					  <button type="button" class="btn btn-default" title="删除" onclick="deleteGoods()" ><i class="fa fa-trash-o"></i> 删除</button>
					  <button type="button" class="btn btn-default" title="审核通过" onclick="updateGoods(1)" ><i class="fa fa-check"></i> 审核通过</button>
					  <button type="button" class="btn btn-default" title="驳回" onclick="updateGoods(2)" ><i class="fa fa-ban"></i> 驳回</button>
					  <button type="button" class="btn btn-default" title="刷新" onclick="window.location.reload();"><i class="fa fa-refresh"></i> 刷新</button>
				  </div>
			  </div>
		  </div>
		  <div class="box-tools pull-right">
			  <div class="has-feedback">
				  商品名称：<input type="text" id="goodsName">
				  <button class="btn btn-default" onclick="selectObj()">查询</button>
			  </div>
		  </div>
		  <!--工具栏/-->
		  <!--数据列表-->
		  <table id="dataList" class="table table-bordered table-striped table-hover dataTable">
			  <thead>
			  <tr>
				  <th class="" style="padding-right:0px">
					  <input id="selall" type="checkbox" class="icheckbox_square-blue">
				  </th>
				  <th class="sorting_asc">商品ID</th>
				  <th class="sorting">商品名称</th>
				  <th class="sorting">商品价格</th>
				  <th class="sorting">一级分类</th>
				  <th class="sorting">二级分类</th>
				  <th class="sorting">三级分类</th>
				  <th class="sorting">状态</th>
				  <th class="text-center">操作</th>
			  </tr>
			  </thead>
			  <tbody id="goodsTable">

			  </tbody>
		  </table>
		  <!--数据列表/-->
	  </div>
	  <!-- 数据表格 /-->
  </div>
  <!-- /.box-body -->
  <div th:insert="/common/page"></div>
</body>

</html>

<script>
	$(function (){
		selectObj()
	})

	function selectObj(){
		$.ajax({
			url:"/goodsController/selectAll",
			type:"post",
			data:{goodsName:$("#goodsName").val(),pageNum:$("[name=pageNum]").val(),pageSize:$("[name=pageSizze]").val()},
			dataType:"json",
			success:function (result){
				if(result.code == 100){
					var goodsList = result.data.page.list;
					var itemList = result.data.list;
					var goodsDiv = ''
					for(var i = 0;i < goodsList.length; i++){
						goodsDiv += '<tr>'
							goodsDiv += '<td><input type="checkbox" name="box" value="'+goodsList[i].id+'"></td>'
							goodsDiv += '<td>'+goodsList[i].id+'</td>'
							goodsDiv += '<td>'+goodsList[i].goodsName+'</td>'
							goodsDiv += '<td>'+goodsList[i].price+'</td>'
                        var category1;
                        var category2;
                        var category3;
                        for(var j = 0; j <itemList.length;j++){
                            if(itemList[j].id == goodsList[i].category1Id){
                                category1 = itemList[j].name;
                            }else if(itemList[j].id == goodsList[i].category2Id){
                                category2 = itemList[j].name;
                            }else if(itemList[j].id == goodsList[i].category3Id){
                                category3 = itemList[j].name;
                            }

                        }
							goodsDiv += '<td>'+category1+'</td>'
							goodsDiv += '<td>'+category2+'</td>'
							goodsDiv += '<td>'+category3+'</td>'
							goodsDiv += '<td>'
                        var auditStatus ;
                        if(goodsList[i].auditStatus == 0){
                            auditStatus = "未审核";
                        }
                        if(goodsList[i].auditStatus == 1){
                            auditStatus = "审核通过";
                        }
                        if(goodsList[i].auditStatus == 2){
                            auditStatus = "审核未通过";
                        }
                        goodsList[i].auditStatus
                        goodsDiv += '<span>'+auditStatus+'</span>'
							goodsDiv += '</td>'
							goodsDiv += '<td class="text-center">'
								goodsDiv += '<button type="button" class="btn bg-olive btn-xs" >详情</button>'
							goodsDiv += '</td>'
						goodsDiv += '</tr>'
					}
					$("#goodsTable").html(goodsDiv);
					buildPage(result.data.page);
				}
				if(result.code == 101){

				}
			},
			error:function (){
				alert("查询网络异常");
			}
		})
	}

	$("#selall").click(function (){
		$("[name=box]").each(function (i,e){
			$(e).prop("checked",$("#selall").prop("checked"));
		})
	})

    function deleteGoods(){
	    var ids = [];
	    $("input[name=box]:checked").each(function (i,e){
	        ids.push($(e).val());
        })
        if(ids.length <= 0){
            alert("请选择要删除的数据");
        }
        $.ajax({
            url:"/goodsController/deleteGoods",
            type:"post",
            data:{ids:ids},
            dataType:"json",
            success:function (result){
                if(result.code == 100){
                    selectObj();
                }
                if(result.code == 101){
                    alert(result.message);
                }
            },
            error:function (){
                alert("删除网络异常");
            }
        })
    }

    function updateGoods(obj){
        var ids = [];
        $("input[name=box]:checked").each(function (i,e){
            ids.push($(e).val());
        })
        if(ids.length <= 0){
            alert("请选择要修改的数据");
        }
        $.ajax({
            url:"/goodsController/updateGoods",
            type:"post",
            data:{ids:ids,auditStatus:obj},
            dataType:"json",
            success:function (result){
                if(result.code == 100){
                    selectObj();
                }
                if(result.code == 101){
                    alert(result.message);
                }
            },
            error:function (){
                alert("删除网络异常");
            }
        })
    }

</script>